<template>
  <div class="page-container">
    <!--表格内容栏-->
    <cm-table
      ref="dataTable"
      pkName="id"
      :filters="filters"
      :filterFields="filterFields"
      :columns="columns"
      @handleTableOperator="handleTableOperator"
      @handleCellOperator="handleCellOperator"
    ></cm-table>

    <!--新增/编辑/查看界面-->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="500px"
      :close-on-click-modal="false"
      destroy-on-close
      draggable
    >
      <el-form
        ref="dataForm"
        label-width="100px"
        label-position="right"
        :model="formData"
        :disabled="!dialogEdit"
        :rules="rules"
        :hide-required-asterisk="!dialogEdit"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="序名" prop="name">
              <el-input v-model="formData.name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="标识" prop="flagId">
              <el-input v-model="formData.flagId"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="当前前缀" prop="curPrefix">
              <el-input v-model="formData.curPrefix"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="当前值" prop="currNumber">
              <el-input v-model="formData.currNumber"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="前缀" prop="prefix">
              <el-input v-model="formData.prefix"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="后缀" prop="suffix">
              <el-input v-model="formData.suffix"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="长度" prop="totalBit">
              <el-input v-model="formData.totalBit"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="!dialogEdit">
          <el-col :span="24">
            <el-form-item label="创建时间" prop="createTime">
              <el-input v-model="formData.createTime"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button
          v-if="dialogEdit"
          size="large"
          type="info"
          @click="dialogVisible=false"
        >取消</el-button>
        <el-button
          v-if="dialogEdit"
          size="large"
          type="primary"
          @click="submitForm"
          :loading="editLoading"
        >提交</el-button>
        <el-button
          v-if="!dialogEdit"
          size="large"
          type="info"
          @click="dialogVisible=false"
        >关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {ref, nextTick} from 'vue'
import {ElMessage, ElMessageBox} from "element-plus";
import api from '@/http/api'

const filters = ref({
  name: ''
})
// 分页查询条件
const filterFields = ref([
  {prop: "name", label: "序名"}
])
  // 分页列表字段
const columns = ref([
  {prop: "name", label: "序名", minWidth: 110, click: true},
  {prop: "flagId", label: "标识", minWidth: 100},
  {prop: "curPrefix", label: "当前前缀", minWidth: 120},
  {prop: "currNumber", label: "当前序号", width: 110, type: 'number'},
  {prop: "prefix", label: "前缀", minWidth: 120},
  {prop: "suffix", label: "后缀", width: 80},
  {prop: "totalBit", label: "长度", width: 80, type: 'number'}
])
  // 表单数据验证规则
const rules = ref({
  name: [
    {required: true, message: '请输入序名', trigger: 'blur'},
    {max: 50, message: '长度必须小于等于 50 个字符！', trigger: 'blur'}
  ],
    flagId: [
    {required: true, message: '请输入标识', trigger: 'blur'},
    {max: 20, message: '长度必须小于等于 20 个字符！', trigger: 'blur'}
  ],
    curPrefix: [
    // {required: true, message: '请输入当前前缀', trigger: 'blur'},
    {max: 20, message: '长度必须小于等于 20 个字符！', trigger: 'blur'}
  ],
    currNumber: [
    // {required: true, message: '请输入当前值', trigger: 'blur'}
  ],
    prefix: [
    {required: true, message: '请输入前缀', trigger: 'blur'},
    {max: 20, message: '长度必须小于等于 20 个字符！', trigger: 'blur'}
  ],
    suffix: [
    // {required: true, message: '请输入后缀', trigger: 'blur'},
    {max: 20, message: '长度必须小于等于 20 个字符！', trigger: 'blur'}
  ],
    totalBit: [
    {required: true, message: '请输入长度', trigger: 'blur'}
  ],
})

const dataForm = ref()
const formData = ref({}) // 弹窗表单数据

const dialogTitle = ref('') // 新增/编辑/查看弹窗标题
const dialogVisible = ref(false) // 弹窗是否显示
const dialogEdit = ref(true) // 弹窗表单是否可编辑
const editLoading = ref(false) // 弹窗表单提交状态
const dataTable = ref() // 弹窗表单提交状态

const handleCellOperator = (property, value, row) => {
  formData.value = Object.assign({}, row);
  dialogVisible.value = true;
  dialogEdit.value = false;
  dialogTitle.value = "查看";

  nextTick(() => {
    dataForm.value && dataForm.value.clearValidate();
  });
}

// 显示操作界面
const handleTableOperator = (button, row) => {
  dialogVisible.value = true;
  dialogEdit.value = true;
  dialogTitle.value = button.name;
  if (button.buttonType === 'edit') {
    formData.value = Object.assign({}, row, {button: button});
  } else {
    formData.value = {button: button};
  }

  nextTick(() => {
    dataForm.value && dataForm.value.clearValidate();
  });
}

// 表单提交
const submitForm = () => {
  dataForm.value && dataForm.value.validate((valid) => {
    if (valid) {
      ElMessageBox.confirm('请确认是否提交？', '提示', {type: 'info'}).then(() => {
        editLoading.value = true;
        api.post(formData.value.button.dataDestUrl, formData.value).then(res => {
          ElMessage.success(res.message);
          dialogVisible.value = false;
          dataTable.value.findPage();
          editLoading.value = false;
        }).catch(() => {
          editLoading.value = false;
        });
      }).catch(()=>{});
    } else {
      ElMessage.error('数据录入不正确，请重新检查！');
    }
  })
}
</script>